import mongoose from '../config/DBHelpler'
import moment from 'moment'

const Schema = mongoose.Schema

const CommentsSchema = new mongoose.Schema({
  cid: { type: String },
  uid: { type: String },
  created: { type: Date },
})
CommentsSchema.pre('save', function (next) {
  this.created = moment().format('YYYY-MM-DD HH:mm:ss')
  next()
})
// CommentsSchema.pre('update', function (next) {
//   this.update = moment().format('YYYY-MM-DD HH:mm:ss')
//   next()
// })
CommentsSchema.post('save', function (err, doc, next) {
  if (err.name === 'MongoError' && err.code === 11000) {
    next(new Error('Error: Monngose has a duplicate key.'))
  } else {
    next(err)
  }
})

CommentsSchema.statics = {
  findByID: function (id) {
    return this.find({ tid: id })
  },
  findByCid: function (cid, ccid) {
    return this.find({ cid: cid, uid: ccid })
  },
}
const CommentsHands = mongoose.model('comments_hands', CommentsSchema)

export default CommentsHands
